<template>
    <div class="app-container calendar-list-container">

        <!-- 查询和其他操作 -->
        <div class="filter-container">
          <el-input clearable class="filter-item" style="width: 200px;" placeholder="类目名称" v-model="listQuery.name"></el-input>
          <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
          <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-edit">添加</el-button>
        </div>

        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
          <el-table-column align="center" min-width="200px" label="平台编号" prop="platformId">
          </el-table-column>
          <el-table-column align="center" min-width="200px" label="商户账号" prop="merId">
          </el-table-column>
          <el-table-column align="center" min-width="200px" label="菜品类目编号" prop="categoryId">
          </el-table-column>       
          <el-table-column align="center" min-width="200px" label="类目名称" prop="name">
          </el-table-column>   
          <el-table-column align="center" label="操作" width="250" >
              <template slot-scope="scope">
                <el-button type="danger" size="mini"  @click="handleDelete(scope.row)">删除</el-button>
                <el-button type="danger" size="mini"  @click="handleUpdate(scope.row)">修改</el-button>
              </template>
          </el-table-column>     
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </div>

        <!-- 修改对话框 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="addDialogFormVisible">
          <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
            <el-form-item label="平台编号" prop="platformId">
              <el-input v-model="dataForm.platformId" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="商户账号" prop="merId">
              <el-input v-model="dataForm.merId" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="类目名称" prop="name"> 
              <el-input v-model="dataForm.name"></el-input>
            </el-form-item>                  
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogFormVisible = false">取消</el-button>
            <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData('dataForm')">确定</el-button>
            <el-button v-if="dialogStatus == 'update'" type="primary" @click="updateData('dataForm')">确定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { listDishesCategoryInfo, addDishesCategoryInfo, updateDishesCategoryInfo, deleteDishesCategoryInfo } from '@/api/cms/restaurant/dishesCategory'
import { mapGetters } from 'vuex'

export default {
  name: 'auction',
  directives: {
    waves
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        name: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      addDialogFormVisible: false,
      downloadLoading: false,
      dataForm: {
        platformId: '',
        merId: '',
        name: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入类目名称',
            trigger: 'blur'
          }
        ]
      },
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogStatus: ''
    }
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'platformId'
    ])
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      listDishesCategoryInfo(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleCreate() {
      this.resetForm()
      this.dataForm.platformId = this.platformId
      this.dialogStatus = 'create'
      this.addDialogFormVisible = true
    },
    createData(formName) {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addDishesCategoryInfo(this.dataForm).then(response => {
            this.addDialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将删除选中项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteDishesCategoryInfo(row)
            .then(response => {
              console.log(response)
              if (response.success) {
                this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              }
            })
        })
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dataForm = row
      this.addDialogFormVisible = true
    },
    updateData(formName) {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDishesCategoryInfo(this.dataForm).then(response => {
            if (response.success) {
              this.addDialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    resetForm() {
      this.dataForm = {
        platformId: '',
        merId: '',
        name: ''
      }
    }
  }
}
</script>

<style>

</style>


